#include "Worker.h"
#include "QDebug"
#include "QThread"

Worker::Worker(QObject *parent) : QObject(parent)
{
    QList<QSerialPortInfo> info = QSerialPortInfo::availablePorts();
    m_Port = new QSerialPort(info.at(0));

    m_Port->setReadBufferSize(1024*1024*10);
    m_Port->setBaudRate(500000000);
    qDebug()<<m_Port->portName()<<m_Port->open(QIODevice::ReadWrite);
    connect(m_Port,SIGNAL(readyRead()),this,SLOT(doWork()));

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()),this,SLOT(timeOut()));
    timer->start(1000);

    qDebug()<<sizeof(PixArray);
}

Worker::~Worker()
{
    m_Port->close();
}
#define MIN_LEN 1280
void Worker::doWork(void)
{
    recive_flag = true;
    static uint32_t rand=0;
    static uint32_t pixsize=0;
    static uint32_t oldrand=0;
    uint32_t rcount=0;
    quint16 r,g,b,a;
    quint16 *data16p=NULL;
    quint8 *data8p=NULL;
    quint16 rowcount[4]={0};
    QByteArray array = m_Port->readAll();
    int32_t size=array.size();
    int32_t len=0;

    m_usbSpeed+=size;
    data16p = (quint16*)array.data();
    data8p = (quint8*)array.data();
    switch(size)
    {
        case 1284*4:
            rowcount[3] = data16p[1284+642]-1;
            if(rowcount[3]<IMAGE_HIGHT){
                pixsize = IMAGE_WIDTH*4*rowcount[3];
                memcpy(m_imgData[rand].img8Data+pixsize,data8p+1284*3+4,MIN_LEN);

            }
        case 1284*3:
            rowcount[2] = data16p[1284]-1;
            if(rowcount[2]<IMAGE_HIGHT){
                pixsize = IMAGE_WIDTH*4*rowcount[2];
                memcpy(m_imgData[rand].img8Data+pixsize,data8p+1284*2+4,MIN_LEN);
                data8p+=1284;
            }
        case 1284*2:
            rowcount[1] = data16p[642]-1;
            if(rowcount[1]<IMAGE_HIGHT){
                pixsize = IMAGE_WIDTH*4*rowcount[1];
                memcpy(m_imgData[rand].img8Data+pixsize,data8p+1284*1+4,MIN_LEN);
                data8p+=1284;
            }
        case 1284:
            rowcount[0] = data16p[0]-1;
            rcount = data16p[1];
            if(rowcount[0]<IMAGE_HIGHT){
                pixsize = IMAGE_WIDTH*4*rowcount[0];
                memcpy(m_imgData[rand].img8Data+pixsize,data8p+4,MIN_LEN);
            }
//            qDebug()<<size<<rowcount[0]<<rcount<<QString::number(data16p[2],16)<<QString::number(data16p[3],16) \
//                    <<QString::number(data16p[92],16)<<QString::number(data16p[93],16);
        break;

        default:break;
    }

    for(int i=0;i<4;i++){
        if(rowcount[i]==241){
            rand++;
            if(rand>4)rand = 0;
//            qDebug()<<rowcount[i];
            rowcount[i]=0;
            break;
        }

    }


//    /* ... here is the expensive or blocking operation ... */
    if(oldrand != rand){
//        quint32 llod=0;
//        if(oldrand>0){
//            llod = oldrand-1;
//        }else{
//            llod = 4;
//        }

        for(int i=0;i<IMAGE_WIDTH*IMAGE_HIGHT;i++){
            r = m_imgData[oldrand].img32Data[i]&0x1F;
            g = ((m_imgData[oldrand].img32Data[i]&0x1C0)>>1);
            b = r|g;
            r = (m_imgData[oldrand].img32Data[i]>>8)&0x1F00;
            g = (((m_imgData[oldrand].img32Data[i]>>8)&0x1C000)>>1);
            m_imgData[oldrand].img16Data[i] = b|g|r;
        }

        emit resultReady(m_imgData[oldrand].img8Data,IMAGE_WIDTH*IMAGE_HIGHT*4);
        oldrand = rand;
    }



}

void Worker::timeOut()
{
    if(!recive_flag){
        qDebug()<<"重新启动串口";
        m_Port->close();
        m_Port->open(QIODevice::ReadWrite);
    }else{
        recive_flag = 0;
    }
    qDebug()<<m_usbSpeed/1024/1024<<"MB/s";
    m_usbSpeed = 0;
}
